﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HY.Common
{
    public class PrintHelper
    {
        /// <summary>
        /// 旋转为横向打印
        /// </summary>
        /// <param name="userControl"></param>
        /// <param name="description"></param>
        private static void PrintRorate(UserControl userControl, string description)
        {
            var rotateTransform = new RotateTransform(90);
            userControl.LayoutTransform = rotateTransform;
            //刷新UI
            var size = new Size(userControl.ActualHeight, userControl.ActualWidth);
            userControl.Measure(size);
            userControl.Arrange(new Rect(new Point(0, 0), size));
            //设置对齐方式
            userControl.HorizontalAlignment = HorizontalAlignment.Left;
            userControl.VerticalAlignment = VerticalAlignment.Top;
            //更新布局
            userControl.UpdateLayout();
            //开始打印
            PrintNormal(userControl, description);
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="userControl">要打印的控件</param>
        /// <param name="description">打印描述</param>
        public static void PrintNormal(UserControl userControl, string description)
        {
            //var localPrintServer = new LocalPrintServer();
            //var printQueue = localPrintServer.DefaultPrintQueue;
            //if (printQueue.IsInError)
            //{
            //    throw new Exception("打印机处于错误状态");
            //}
            //var printDialog = new PrintDialog
            //{
            //    PrintQueue = printQueue, //打印队列
            //    PrintTicket = { CopyCount = copyCount } //打印个数
            //};
            //设置纸张大小
            //var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth);
            //var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight);
            //printDialog.PrintTicket.PageMediaSize = new PageMediaSize(pageWidth, pageHeight);

            PrintDialog printDialog = new PrintDialog();
            printDialog.PrintVisual(userControl, description);
        }
    }
}
